﻿Imports System.Windows.Forms

Public Class dlgValveProfile

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub dlgValveProfile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        displayNumberValves()

        'update valve label names
        cbV1.Text = My.Settings.asV1
        cbV2.Text = My.Settings.asV2
        cbV3.Text = My.Settings.asV3
        cbV4.Text = My.Settings.asV4
        cbV5.Text = My.Settings.asV5
        cbV6.Text = My.Settings.asV6
        cbV7.Text = My.Settings.asV7
        cbV8.Text = My.Settings.asV8
        cbV9.Text = My.Settings.asV9
        cbV10.Text = My.Settings.asV10
        cbV11.Text = My.Settings.asV11
        cbV12.Text = My.Settings.asV12
        cbV13.Text = My.Settings.asV13
        cbV14.Text = My.Settings.asV14
        cbV15.Text = My.Settings.asV15
        cbV16.Text = My.Settings.asV16
        cbV17.Text = My.Settings.asV17
        cbV18.Text = My.Settings.asV18
        cbV19.Text = My.Settings.asV19
        cbV20.Text = My.Settings.asV20
        cbV21.Text = My.Settings.asV21
        cbV22.Text = My.Settings.asV22
        cbV23.Text = My.Settings.asv23
        cbV24.Text = My.Settings.asV24
        cbV25.Text = My.Settings.asV25
        cbV26.Text = My.Settings.asV26
        cbV27.Text = My.Settings.asV27
        cbV28.Text = My.Settings.asV28
        cbV29.Text = My.Settings.asV29
        cbV30.Text = My.Settings.asV30
        cbV31.Text = My.Settings.asV31
        cbV32.Text = My.Settings.asV32

        'if any profiles are unknown, issue command on BT to retrieve profiles
        If lblVP0.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+0-")
        End If
        If lblVP1.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+1-")
        End If
        If lblVP2.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+2-")
        End If
        If lblVP3.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+3-")
        End If
        If lblVP4.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+4-")
        End If
        If lblVP5.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+5-")
        End If
        If lblVP6.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+6-")
        End If
        If lblVP7.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+7-")
        End If
        If lblVP8.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+8-")
        End If
        If lblVP9.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+9-")
        End If
        If lblVP10.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+10-")
        End If
        If lblVP11.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+11-")
        End If
        If lblVP12.Text = "[unknown]" Then
            AddToTXstack("GET_VLVCFG+12-")
        End If

        'set Fill HLT profile as active
        rbVP0.Checked = True
    End Sub

    Private Sub cboNumValves_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboNumValves.SelectedIndexChanged
        Settings.asBTnumValves = cboNumValves.Text
        DisplayNumberValves()
    End Sub

    Private Sub rbVP1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP1.CheckedChanged
        If rbVP1.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPfillMash) Then intValveBits = CInt(Settings.VPfillMash)
            UpdateValveDisplay(intValveBits)
        End If
    End Sub

    Private Sub UpdateValveDisplay(ByVal BitMask As Integer)

        'update buttons to reflect current profile
        Dim ValveOutput As System.Windows.Forms.CheckBox
        Dim vlvnum As Integer = 0

        For Each ValveOutput In gbValveProfiler.Controls()
            vlvnum = ValveOutput.TabIndex + 1
            If ExamineBit(BitMask, vlvnum) Then
                ValveOutput.Checked = True
            Else
                ValveOutput.Checked = False
            End If
        Next

    End Sub

    Private Sub rbVP0_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP0.CheckedChanged
        If rbVP0.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPfillHLT) Then intValveBits = CInt(Settings.VPfillHLT)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub rbVP2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP2.CheckedChanged
        If rbVP2.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPaddGrain) Then intValveBits = CInt(Settings.VPaddGrain)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub rbVP3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP3.CheckedChanged
        If rbVP3.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPmashHeat) Then intValveBits = CInt(Settings.VPmashHeat)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub rbVP4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP4.CheckedChanged
        If rbVP4.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPmashIdle) Then intValveBits = CInt(Settings.VPmashIdle)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub rbVP5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP5.CheckedChanged
        If rbVP5.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPspargeIn) Then intValveBits = CInt(Settings.VPspargeIn)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub rbVP6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP6.CheckedChanged
        If rbVP6.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPspargeOut) Then intValveBits = CInt(Settings.VPspargeOut)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub rbVP7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP7.CheckedChanged
        If rbVP7.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPboilAddition) Then intValveBits = CInt(Settings.VPboilAddition)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub rbVP8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP8.CheckedChanged
        If rbVP8.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPkettleLid) Then intValveBits = CInt(Settings.VPkettleLid)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub rbVP9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP9.CheckedChanged
        If rbVP9.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPchillerWater) Then intValveBits = CInt(Settings.VPchillerWater)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub rbVP10_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP10.CheckedChanged
        If rbVP10.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPchillerBeer) Then intValveBits = CInt(Settings.VPchillerBeer)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub rbVP11_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP11.CheckedChanged
        If rbVP11.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPboilRecirc) Then intValveBits = CInt(Settings.VPboilRecirc)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub RadioButton13_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbVP12.CheckedChanged
        If rbVP12.Checked Then
            'update profile display
            Dim intValveBits As Integer = 0
            If IsNumeric(Settings.VPdrain) Then intValveBits = CInt(Settings.VPdrain)
            UpdateValveDisplay(intValveBits)
        End If

    End Sub

    Private Sub DisplayNumberValves()

        'show the right number of valves
        If Settings.asBTnumValves = 8 Then
            'show 8 outputs
            cbV9.Visible = False
            cbV10.Visible = False
            cbV11.Visible = False
            cbV12.Visible = False
            cbV13.Visible = False
            cbV14.Visible = False
            cbV15.Visible = False
            cbV16.Visible = False
            cbV17.Visible = False
            cbV18.Visible = False
            cbV19.Visible = False
            cbV20.Visible = False
            cbV21.Visible = False
            cbV22.Visible = False
            cbV23.Visible = False
            cbV24.Visible = False
            cbV25.Visible = False
            cbV26.Visible = False
            cbV27.Visible = False
            cbV28.Visible = False
            cbV29.Visible = False
            cbV30.Visible = False
            cbV31.Visible = False
            cbV32.Visible = False

        ElseIf Settings.asBTnumValves = 11 Then
            'show 11 outputs
            cbV9.Visible = True
            cbV10.Visible = True
            cbV11.Visible = True
            cbV12.Visible = False
            cbV13.Visible = False
            cbV14.Visible = False
            cbV15.Visible = False
            cbV16.Visible = False
            cbV17.Visible = False
            cbV18.Visible = False
            cbV19.Visible = False
            cbV20.Visible = False
            cbV21.Visible = False
            cbV22.Visible = False
            cbV23.Visible = False
            cbV24.Visible = False
            cbV25.Visible = False
            cbV26.Visible = False
            cbV27.Visible = False
            cbV28.Visible = False
            cbV29.Visible = False
            cbV30.Visible = False
            cbV31.Visible = False
            cbV32.Visible = False
        ElseIf Settings.asBTnumValves = 16 Then
            'show 16 outputs
            cbV9.Visible = True
            cbV10.Visible = True
            cbV11.Visible = True
            cbV12.Visible = True
            cbV13.Visible = True
            cbV14.Visible = True
            cbV15.Visible = True
            cbV16.Visible = True
            cbV17.Visible = False
            cbV18.Visible = False
            cbV19.Visible = False
            cbV20.Visible = False
            cbV21.Visible = False
            cbV22.Visible = False
            cbV23.Visible = False
            cbV24.Visible = False
            cbV25.Visible = False
            cbV26.Visible = False
            cbV27.Visible = False
            cbV28.Visible = False
            cbV29.Visible = False
            cbV30.Visible = False
            cbV31.Visible = False
            cbV32.Visible = False
        ElseIf Settings.asBTnumValves = 24 Then
            'show 24 outputs
            cbV9.Visible = True
            cbV10.Visible = True
            cbV11.Visible = True
            cbV12.Visible = True
            cbV13.Visible = True
            cbV14.Visible = True
            cbV15.Visible = True
            cbV16.Visible = True
            cbV17.Visible = True
            cbV18.Visible = True
            cbV19.Visible = True
            cbV20.Visible = True
            cbV21.Visible = True
            cbV22.Visible = True
            cbV23.Visible = True
            cbV24.Visible = True
            cbV25.Visible = False
            cbV26.Visible = False
            cbV27.Visible = False
            cbV28.Visible = False
            cbV29.Visible = False
            cbV30.Visible = False
            cbV31.Visible = False
            cbV32.Visible = False
        ElseIf Settings.asBTnumValves = 32 Then
            'show all 32 outputs
            cbV9.Visible = True
            cbV10.Visible = True
            cbV11.Visible = True
            cbV12.Visible = True
            cbV13.Visible = True
            cbV14.Visible = True
            cbV15.Visible = True
            cbV16.Visible = True
            cbV17.Visible = True
            cbV18.Visible = True
            cbV19.Visible = True
            cbV20.Visible = True
            cbV21.Visible = True
            cbV22.Visible = True
            cbV23.Visible = True
            cbV24.Visible = True
            cbV25.Visible = True
            cbV26.Visible = True
            cbV27.Visible = True
            cbV28.Visible = True
            cbV29.Visible = True
            cbV30.Visible = True
            cbV31.Visible = True
            cbV32.Visible = True
        End If

    End Sub

    Private Sub cbV1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbV1.CheckedChanged
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Enabled = False
        blnAllowVPchanges = True

        lblStatus.Text = "Valve profile updated."
        lblStatus.ForeColor = Color.Green
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        'We use the Update button to send profile data to the BT
        If blnAllowVPchanges Then
            'send profile update
            Dim intValveBits As Int64 = 0

            Dim ValveOutput As System.Windows.Forms.CheckBox
            Dim vlvnum As Integer = 0

            For Each ValveOutput In gbValveProfiler.Controls()
                If ValveOutput.Checked = True Then
                    vlvnum = ValveOutput.TabIndex
                    intValveBits += 2 ^ vlvnum
                End If
            Next

            'work out which profile is selected
            Dim ProfileOption As System.Windows.Forms.RadioButton
            Dim prfnum As Integer = 12

            For Each ProfileOption In gbProfiles.Controls
                If ProfileOption.Checked Then
                    Exit For
                End If
                prfnum -= 1
            Next

            'and send profile update command
            Dim strMsg As String
            strMsg = "SET_VLVCFG+" & CStr(prfnum) & "+" & CStr(intValveBits) & "-"
            AddToTXstack(strMsg)

            blnAllowVPchanges = False   'wait until next profile update comes through before more changes can be made
            lblStatus.Text = "Saving profile, please wait ..."
            lblStatus.ForeColor = Color.Red
            Timer1.Enabled = True

        End If

    End Sub

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        'set valves on BT to match current profile
        Dim intValveBits As Int64 = 0

        'clear all valves first
        intValveBits = 2 ^ 32
        AddToTXstack("SET_VLV+" & CStr(intValveBits) & "+0-")

        intValveBits = 0

        Dim ValveOutput As System.Windows.Forms.CheckBox
        Dim vlvnum As Integer = 0

        For Each ValveOutput In gbValveProfiler.Controls()
            If ValveOutput.Checked = True Then
                vlvnum = ValveOutput.TabIndex
                intValveBits += 2 ^ vlvnum
            End If
        Next

        'then set the current ones
        AddToTXstack("SET_VLV+" & CStr(intValveBits) & "+1-")

        lblStatus.Text = "Testing valves on BT ..."
        lblStatus.ForeColor = Color.Violet

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'clear all valves 
        Dim intValveBits As Int64
        intValveBits = 2 ^ 32
        AddToTXstack("SET_VLV+" & CStr(intValveBits) & "+0-")

    End Sub
End Class
